<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" :title="title" />
</template>

<script>
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe',
      color: ['#58D9F9', 'orange', 'yellow', 'lightgreen']
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        color: this.color,
        title: {
          text: this.title,
          top: '1%',
          left: '5%',
          textStyle: {
            fontSize: 16,
            color: this.fontColor
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          show: false,
          top: '8%',
          right: '3%',
          textStyle: {
            fontSize: 14,
            color: this.fontColor
          }
        },
        grid: {
          top: '20%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          boundaryGap: true,
          axisLabel: {
            color: this.fontColor,
            interval: 0
          },
          data: []
        }],
        yAxis: [{
          type: 'value',
          name: '',
          minInterval: 1,
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }, {
          type: 'value',
          name: '贡献度',
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor,
            formatter: '{value}%'
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }],
        series: [{
          name: '不良',
          type: 'bar',
          smooth: true,
          label: {
            show: false,
            position: 'top',
            color: this.fontColor
          },
          data: [220, 191, 182, 150, 145, 134, 125, 122, 120, 110]
        }, {
          name: '贡献度',
          type: 'line',
          smooth: true,
          yAxisIndex: 1,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor,
            formatter: '{c}%'
          },
          data: ['7.3', '8.0', '8.1', '8.3', '8.9', '9.7', '10.0', '12.1', '12.7', '14.7']
        }]
      })
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
